<?php
namespace App\Admin\Models;
use Common\BaseModel;
class HomeTemplatesConfig extends BaseModel
{
    protected $res;
    protected $redis_key="admin_";
    public function initialize()
    {
        parent::initialize();
        $this->setSource('view_home_templates_config');
    }

    public function find_list($params=[]){
        $page = isset($params['page']) ? $params['page'] : 1;
        $size = 10;
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }
        $phql = "SELECT config.id,config.is_common,config.name,config.search_id,config.carousel_id,config.menu_id,config.ad_id,config.ad1_id,config.ad2_id,config.background_image_id,config.is_defalut,config.platform,config.version,config.remark,config.carousel_background_image_id,config.menu_background_image_id,config.font_color,config.current_color,searchbar.name as searchBarName,HomeCarousel.name as carouselName,HomeMenu.name as menuName,adconfig.name as ad_configname,adconfig1.name as ad_config1name,SourceMaterial.path as background_image,adconfig2.name as ad_config2name
                FROM view_home_templates_config as config
                LEFT JOIN view_home_search_bar as searchbar ON config.search_id = searchbar.id
                LEFT JOIN view_home_carousel as HomeCarousel ON config.carousel_id = HomeCarousel.id
                LEFT JOIN view_home_menu as HomeMenu ON config.menu_id = HomeMenu.id
                LEFT JOIN view_home_ad_cofig as adconfig ON config.ad_id = adconfig.id
                LEFT JOIN view_home_ad_cofig as adconfig1 ON config.ad1_id = adconfig1.id
                LEFT JOIN view_home_ad_cofig as adconfig2 ON config.ad2_id = adconfig2.id
                LEFT JOIN source_material as SourceMaterial ON config.background_image_id = SourceMaterial.id
				ORDER BY config.id DESC
                limit $page,$size";
        $rows = $this->getDI()->get('dbMaster')->fetchAll($phql);
        return $rows;
    }


    /**
     * 根据不同的版本来获取模板
     */
    public function versionlist($type,$data=[]){
        $page = isset($data['page']) ? $data['page'] : 1;
        $size = 10;
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }
        $conditions='1=1';
        switch ($type){
            case 'search_bar':
                $mysql = "SELECT HomeTemplatesConfig.id,HomeTemplatesConfig.name,HomeTemplatesConfig.search_id as commonname_id,HomeTemplatesConfig.carousel_id,HomeTemplatesConfig.menu_id,HomeTemplatesConfig.ad_id,HomeTemplatesConfig.background_image_id,HomeTemplatesConfig.is_defalut,HomeTemplatesConfig.platform,HomeTemplatesConfig.version,HomeTemplatesConfig.remark,HomeSearchBar.name as commonname";
                break;
            case 'carousel':
                $mysql = "SELECT HomeTemplatesConfig.id,HomeTemplatesConfig.name,HomeTemplatesConfig.search_id,HomeTemplatesConfig.carousel_id as commonname_id,HomeTemplatesConfig.menu_id,HomeTemplatesConfig.ad_id,HomeTemplatesConfig.background_image_id,HomeTemplatesConfig.is_defalut,HomeTemplatesConfig.platform,HomeTemplatesConfig.version,HomeTemplatesConfig.remark,HomeCarousel.name as commonname";
                break;
            case 'menu':
                $mysql = "SELECT HomeTemplatesConfig.id,HomeTemplatesConfig.name,HomeTemplatesConfig.search_id,HomeTemplatesConfig.carousel_id,HomeTemplatesConfig.menu_id as commonname_id,HomeTemplatesConfig.ad_id,HomeTemplatesConfig.background_image_id,HomeTemplatesConfig.is_defalut,HomeTemplatesConfig.platform,HomeTemplatesConfig.version,HomeTemplatesConfig.remark,HomeMenu.name as commonname";
                break;
            case 'ad':
            case 'ad1':
            case 'ad2':
                $mysql = "SELECT HomeTemplatesConfig.id,HomeTemplatesConfig.name,HomeTemplatesConfig.search_id,HomeTemplatesConfig.carousel_id,HomeTemplatesConfig.menu_id,HomeTemplatesConfig.ad_id as commonname_id,HomeTemplatesConfig.background_image_id,HomeTemplatesConfig.is_defalut,HomeTemplatesConfig.platform,HomeTemplatesConfig.version,HomeTemplatesConfig.remark,HomeAdCofig.name as commonname";
                break;
        }
        if(isset($data['version']) && !empty($data['version'])){
            $conditions .= " AND HomeTemplatesConfig.version='{$data['version']}'";
        }
        $phql = $mysql." 
            FROM view_home_templates_config as HomeTemplatesConfig
            LEFT JOIN view_home_search_bar as HomeSearchBar ON HomeTemplatesConfig.search_id = HomeSearchBar.id
            LEFT JOIN view_home_carousel as HomeCarousel ON HomeTemplatesConfig.carousel_id = HomeCarousel.id
            LEFT JOIN view_home_menu as HomeMenu ON HomeTemplatesConfig.menu_id = HomeMenu.id
            LEFT JOIN view_home_ad_cofig as HomeAdCofig ON HomeTemplatesConfig.ad_id = HomeAdCofig.id
            LEFT JOIN view_home_ad_cofig as HomeAdConfig1 ON HomeTemplatesConfig.ad1_id = HomeAdConfig1.id
            LEFT JOIN view_home_ad_cofig as HomeAdConfig2 ON HomeTemplatesConfig.ad2_id = HomeAdConfig2.id
            LEFT JOIN source_material as SourceMaterial ON HomeTemplatesConfig.background_image_id = SourceMaterial.id
            WHERE $conditions
            ORDER BY HomeTemplatesConfig.id DESC";
        $rows = $this->getDI()->get('dbMaster')->fetchAll($phql);
        return $rows;
    }


    /**
     * 根据配置信息查找连表详情
     * @param $id
     * @return mixed
     */
    public function find_by_id_union($id){
        $phql = "SELECT HomeTemplatesConfig.id,HomeTemplatesConfig.name,HomeTemplatesConfig.search_id,HomeTemplatesConfig.is_defalut,HomeTemplatesConfig.is_common,HomeTemplatesConfig.carousel_id,HomeTemplatesConfig.menu_id,HomeTemplatesConfig.ad1_id,HomeTemplatesConfig.ad_id,HomeTemplatesConfig.platform,HomeTemplatesConfig.version,HomeTemplatesConfig.remark,HomeSearchBar.name as searchBarName,HomeCarousel.name as carouselName,HomeMenu.name as menuName,adconfig.name as adConfigName,adconfig1.name as ad1ConfigName,adconfig2.name as ad2ConfigName
                FROM view_home_templates_config as HomeTemplatesConfig
                LEFT JOIN view_home_search_bar as HomeSearchBar ON HomeTemplatesConfig.search_id = HomeSearchBar.id
                LEFT JOIN view_home_carousel as HomeCarousel ON HomeTemplatesConfig.carousel_id = HomeCarousel.id
                LEFT JOIN view_home_menu as HomeMenu ON HomeTemplatesConfig.menu_id = HomeMenu.id
                LEFT JOIN view_home_ad_cofig as adconfig ON HomeTemplatesConfig.ad_id = adconfig.id
                LEFT JOIN view_home_ad_cofig as adconfig1 ON HomeTemplatesConfig.ad1_id = adconfig1.id
                LEFT JOIN view_home_ad_cofig as adconfig2 ON HomeTemplatesConfig.ad2_id = adconfig2.id
                LEFT JOIN source_material as SourceMaterial ON HomeTemplatesConfig.background_image_id = SourceMaterial.id
                WHERE HomeTemplatesConfig.id={$id}
				ORDER BY HomeTemplatesConfig.id DESC";
        $rows = $this->getDI()->get('dbMaster')->fetchOne($phql);
        return $rows;
    }

    /**
     * @func 根据版本id 查找版本
     * @param $id
     * @return array
     */
    public function find_by_id($id){
        if(empty($id)){
            return [];
        }
        $cate_info = $this->findFirstById($id);
        if($cate_info){
            return $cate_info->toarray();
        }else{
            return [];
        }
    }


    public function search_version_platform($version,$platfrom){
        $conditions = "version = '{$version}' AND platform='{$platfrom}'";
        $is_count = $this->count(array(
            $conditions
        ));
        return $is_count;
    }


    public function save_params($params){
        $result = $this::save($params);
        return $result;
    }

    public function find_by_id_obj($id){
        return $this->findFirstById($id);
    }


    //批量修改
    public function batch_update()
    {
        $updateSql = <<<ENT
UPDATE `view_home_templates_config`
    SET is_defalut = 1,is_common=0
ENT;
        return $this->getDI()->get('dbMaster')->execute($updateSql);
    }



    //批量修改
    public function batch_version_update($version)
    {
        $updateSql = <<<ENT
UPDATE `view_home_templates_config`
    SET is_defalut = 1,is_common=1
WHERE version='{$version}'
ENT;
        return $this->getDI()->get('dbMaster')->execute($updateSql);
    }


}
